package com.netsdk.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

/**
 * 核心工具类
 */
public class CoreUtil {

    private static final String SYSTEM_PATH = System.getProperty("user.dir");

    /**
     * 获取根路径
     * @return String
     */
    public static String getRoot() {
        return new File(SYSTEM_PATH).getAbsolutePath();
    }

    /**
     * 获取根路径
     * @return String
     */
    public static String getRoot_NETSDK() {
        String path = SYSTEM_PATH + "/libs/linux64/libdhnetsdk.so";
        if (isWindows()) {
            path = SYSTEM_PATH + "/libs/win64/dhnetsdk.dll";
        }
        return new File(path).getAbsolutePath();
    }

    /**
     * 获取根路径
     * @return String
     */
    public static String getRoot_CONFIG() {
        String path = SYSTEM_PATH + "/libs/linux64/libdhconfigsdk.so";
        if (isWindows()) {
            path = SYSTEM_PATH + "/libs/win64/dhconfigsdk.dll";
        }
        return new File(path).getAbsolutePath();
    }

    /**
     * 文件转base64
     * @param filePath
     * @return
     */
    public static String filePathToBase64(String filePath) {
        if (filePath == null) {
            return null;
        }
        try {
            byte[] b = Files.readAllBytes(Paths.get(filePath));
            return Base64.getEncoder().encodeToString(b);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取年月日时分秒（yyyyMMddHHmmss）
     * @return String
     */
    public static String getDate() {
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(new Date());
    }

    /**
     * 获取UUID
     * @return String
     */
    public static String getUuid() {
        return java.util.UUID.randomUUID().toString().replaceAll("-","");
    }

    /**
     * 是否是Windows
     * @return boolean
     */
    public static boolean isWindows() {
        return System.getProperty("os.name").contains("Windows");
    }

    /**
     * 获取文本内容
     * @param path
     * @return String
     */
    public static String getContent(String path) {
        try {
            String str;
            StringBuilder sbd = new StringBuilder();
            File file = new File(path);
            FileReader fileReader = new FileReader(file);
            BufferedReader bufReader = new BufferedReader(fileReader);
            while ((str = bufReader.readLine()) != null) {
                sbd.append(str);
            }
            return sbd.toString();
        } catch (IOException ex) {
            return null;
        }
    }

    /**
     * 获取配置文件
     * @return JSONObject
     */
    public static JSONObject getConfigFile() {
        String path = CoreUtil.class.getResource("/").getPath() + "conf.json";
        String content = CoreUtil.getContent(path);
        JSONObject json = JSONObject.parseObject(content);
        // 如果不存在读取 config 外部文件
        if (json == null) {
            String outpath = System.getProperty("user.dir") + File.separator + "conf.json";
            json = JSONObject.parseObject(CoreUtil.getContent(outpath));
        }
        return json;
    }

    /**
     * 读取resources目录配置文件
     * @param path
     * @return
     */
    public static String resourcesFile(String path) {
        InputStream inputStream;
        BufferedReader bufReader;
        InputStreamReader inputReader;

        String str;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            inputStream = new ClassPathResource(path).getInputStream();
            inputReader = new InputStreamReader(inputStream);
            bufReader = new BufferedReader(inputReader);

            while((str = bufReader.readLine()) != null) {
                stringBuilder.append(str);
            }
            bufReader.close();
            inputReader.close();
            inputStream.close();
            return stringBuilder.toString();
        } catch (IOException ex) {
            return null;
        }
    }

}